#!/usr/bin/env python3  /*设定脚本文件直接能执行*/
# -*- coding:utf-8 -*-   /*设置编码*/
"""
@Author: zhuo
@Software: PyCharm
@File: tQGroupBox.py
@Time: 2024/4/18 10:07
@Function：ctQGroupBox：基于QGroupBox和QScrollArea实现带有滚动条的QGroupBox(分组框)
"""
from PySide6 import QtWidgets
from PySide6.QtWidgets import QWidget, QVBoxLayout, QScrollArea, QGroupBox, QPushButton


# 自定义QGroupBox类
class ctQGroupBox(QWidget):
    """
    ctQGroupBox：基于QGroupBox和QScrollArea实现带有滚动条的QGroupBox(分组框)
    """

    # 初始化
    def __init__(self, groupboxName, parent=None):
        """
        function:  初始化
              in:  groupboxName ：QGroupBox的名称
             out:  None
          return:  None
          others:  Initialize
        """
        # 继承自父类QWidget
        super(ctQGroupBox, self).__init__(parent)

        # 创建QVBoxLayout 垂直布局
        self.mainLayout = QVBoxLayout()
        self.setLayout(self.mainLayout)

        # 创建一个QGroupBox
        __m_groupBox = QGroupBox(groupboxName)

        # 创建一个QVBoxLayout用于QGroupBox的内部布局
        self.innerLayout = QVBoxLayout()
        __m_groupBox.setLayout(self.innerLayout)

        # 创建一个QScrollArea并将QGroupBox设置为其内容
        __m_scrollArea = QScrollArea()
        __m_scrollArea.setWidget(__m_groupBox)
        # 允许QGroupBox调整大小以适应滚动区域
        __m_scrollArea.setWidgetResizable(True)

        # 将QScrollArea添加到主窗口的中心部件的布局中
        self.mainLayout.addWidget(__m_scrollArea)


if __name__ == '__main__':
    app = QtWidgets.QApplication()
    window = QtWidgets.QWidget()
    window.resize(500, 500)
    lay = QVBoxLayout()
    window.setLayout(lay)
    win = ctQGroupBox("Group Box with Scroll Bar")
    # 添加多个按钮到QGroupBox中，以模拟内容过多需要滚动的情况
    for i in range(20):
        button = QPushButton(f"Button {i}")
        win.innerLayout.addWidget(button)
    lay.addWidget(win)
    window.show()
    app.exec()
